$(function() {
	InitLeftMenu();
	tabClose();
	tabCloseEven();
	clockon();
	// addTab($("#m12").text(),$("#m12").attr("href"),$("#m12").attr("id"));
})
// 初始化左侧
function InitLeftMenu() {
	$("#menuAccordion").empty();
	var menulist = "";
	$.each(_menus.menus, function(i, n) {
		menulist += '<div title="' + n.menuname + '"  icon="' + n.icon
				+ '" style="overflow:auto;">';
		menulist += '<ul>';
		$.each(n.menus, function(j, o) {
			menulist += '<li><div><a target="mainFrame_m' + o.mid + '" id="m'
					+ o.mid + '" href="' + o.url + '" ><span class="'
					+ o.icon + '" >&nbsp;</span>' + o.menuname + '</a></div></li> ';
		})
		menulist += '</ul></div>';
	});

	$("#menuAccordion").append(menulist);

	$('#menuAccordion li a').click(function() {
		var tabTitle = $(this).text();
		var url = $(this).attr("href");
		var mid = $(this).attr("id");
		addTab(tabTitle, url, mid);
		$('#menuAccordion li div').removeClass("selected");
		$(this).parent().addClass("selected");
	}).hover(function() {
		$(this).parent().addClass("hover");
	}, function() {
		$(this).parent().removeClass("hover");
	});
	$("#menuAccordion").accordion();
}

function addTab(subtitle, url, mid) {
	if (!$('#tabs').tabs('exists', subtitle)) {
		$('#tabs').tabs('add', {
			title : subtitle,
			content : createFrame(url, mid),
			closable : true,
			width : $('#mainPanle').width() - 10,
			height : $('#mainPanle').height() - 26
		});
	} else {
		$('#tabs').tabs('select', subtitle);
	}
	tabClose();
}

function updateTab(subtitle, url, mid) {
	if (!$('#tabs').tabs('exists', subtitle)) {
		$('#tabs').tabs('add', {
			title : subtitle,
			content : createFrame(url, mid),
			closable : true,
			width : $('#mainPanle').width() - 10,
			height : $('#mainPanle').height() - 26

		});
	} else {
		$('#tabs').tabs('select', subtitle);
		refreshTab({
			tabTitle : subtitle,
			url : url
		});
	}
	tabClose();
}



function refreshTab(cfg) {
	var refresh_tab = cfg.tabTitle ? $('#tabs').tabs('getTab', cfg.tabTitle)
			: $('#tabs').tabs('getSelected');
	if (refresh_tab && refresh_tab.find('iframe').length > 0) {
		var _refresh_ifram = refresh_tab.find('iframe')[0];
		var refresh_url = cfg.url ? cfg.url : _refresh_ifram.src;
		_refresh_ifram.contentWindow.location.href = refresh_url;
	}
}

function createFrame(url, mid) {

	var s = '<iframe name="mainFrame_' + mid
			+ '" scrolling="auto" frameborder="0"  src="' + url
			+ '" style="width:100%;height:100%;"></iframe>';
	return s;
}

function S4() {
	return (((1 + Math.random()) * 0x10000) | 0).toString(16).substring(1);
}

function tabClose() {
	/* 双击关闭TAB选项卡 */
	$(".tabs-inner").dblclick(function() {
		var subtitle = $(this).children("span").text();
		$('#tabs').tabs('close', subtitle);
	})

	$(".tabs-inner").bind('contextmenu', function(e) {
		$('#mm').menu('show', {
			left : e.pageX,
			top : e.pageY
		});

		var subtitle = $(this).children("span").text();
		$('#mm').data("currtab", subtitle);

		return false;
	});
}

function closeCurrentTab(){
	var tab = $('#tabs').tabs('getSelected');
	var index = $('#tabs').tabs('getTabIndex',tab);
	$('#tabs').tabs('close', index);
}


// 绑定右键菜单事件
function tabCloseEven() {
	// 关闭当前
	$('#mm-tabclose').click(function() {
		var currtab_title = $('#mm').data("currtab");
		$('#tabs').tabs('close', currtab_title);
	})
	// 全部关闭
	$('#mm-tabcloseall').click(function() {
		$('.tabs-inner span').each(function(i, n) {
			var t = $(n).text();
			if (t != '欢迎使用')
				$('#tabs').tabs('close', t);
		});
	});
	// 关闭除当前之外的TAB
	$('#mm-tabcloseother').click(function() {
		var currtab_title = $('#mm').data("currtab");
		$('.tabs-inner span').each(function(i, n) {
			var t = $(n).text();
			if (t != currtab_title && t != '欢迎使用')
				$('#tabs').tabs('close', t);
		});
	});
	// 关闭当前右侧的TAB
	$('#mm-tabcloseright').click(function() {
		var nextall = $('.tabs-selected').nextAll();
		if (nextall.length == 0) {
			msgShow('提示', '后边没有啦~~', 'info');
			return false;
		}
		nextall.each(function(i, n) {
			var t = $('a:eq(0) span', $(n)).text();
			$('#tabs').tabs('close', t);
		});
		return false;
	});
	// 关闭当前左侧的TAB
	$('#mm-tabcloseleft').click(function() {
		var prevall = $('.tabs-selected').prevAll();
		if (prevall.length == 0) {
			msgShow('提示', '到头了，前边没有啦~~', 'info');
			return false;
		}
		prevall.each(function(i, n) {
			var t = $('a:eq(0) span', $(n)).text();
			if (t != '欢迎使用')
				$('#tabs').tabs('close', t);
		});
		return false;
	});

	// 退出
	$("#mm-exit").click(function() {
		$('#mm').menu('hide');
	})
}
// 弹出信息窗口 title:标题 msgString:提示信息 msgType:信息类型 [error,info,question,warning]
function msgShow(title, msgString, msgType) {
	$.messager.alert(title, msgString, msgType);
}

function clockon() {
	var now = new Date();
	var year = now.getFullYear(); // getFullYear getYear
	var month = now.getMonth();
	var date = now.getDate();
	var day = now.getDay();
	var hour = now.getHours();
	var minu = now.getMinutes();
	var sec = now.getSeconds();
	var week;
	month = month + 1;
	if (month < 10)
		month = "0" + month;
	if (date < 10)
		date = "0" + date;
	if (hour < 10)
		hour = "0" + hour;
	if (minu < 10)
		minu = "0" + minu;
	if (sec < 10)
		sec = "0" + sec;
	var arr_week = new Array("星期日", "星期一", "星期二", "星期三", "星期四", "星期五", "星期六");
	week = arr_week[day];
	var time = "";
	// time = year + "年" + month + "月" + date + "日" + " " + hour + ":" + minu +
	// ":" + sec + " " + week;
	time = year + "年" + month + "月" + date + "日" + " " + week;
	$("#bgclock").html(time);

	// var timer = setTimeout("clockon()", 200);
}
